/**
 * Created by Administrator on 2015/8/14.
 */
var querystring = require('querystring');
var userInfoDao = require('../dao/userInfoDao');
var activeDao = require('../dao/activeDao');
var admitDao = require('../dao/admitDao');
var userDao = require('../dao/userDao');
var activeDetailDao = require('../dao/activeDetailDao');
var adminSuperDao = require('../dao/adminSuperDao');
var page = require('../utils/page');
module.exports = {
    //queryByUser:function(req, res, callback) {
    //    userDao.queryByUser(req, res, function(a){callback(a)});
    //},
    add: function (req, res, callback) {
        userDao.add(req, res, function (result) {
            callback(result)
        });
    },
    addUser: function (req, res, callback) {  //����Ա����û�

        userInfoDao.addUser(req, res, function (userInfoId) {
            userInfoId = userInfoId.content.insertId;
            userDao.addUser(req, res, userInfoId, function (result) {
                callback(result)
            });
        })
    },
    /**管理员删除用户*/
    delUser: function (req, res, callback) {  //����Ա����û�
        userInfoDao.delUser(req, res, function (result) {
            userDao.delUser(result.content, res, function (result) {
                userInfoDao.queryAll(req, res, function (result) {
                    callback(result);
                })
            });
        })
    },

    queryAllUser: function (req,  callback) {
        var pageNo,pageSize=6;
        var beginIndex;
        if (req.param('pageNo') == undefined) {
            pageNo = 1;
        } else {
            pageNo = req.param('pageNo');
        }
        beginIndex = (pageNo - 1) * pageSize;

        userInfoDao.queryAll(req, pageNo, beginIndex, pageSize, function (result, pageNo) {
            if (result.length >= 1) {
                result = {
                    code: 200,
                    content: result
                };
                callback(result, pageNo);
            } else {
                callback("no");
            }
        });
    },
    queryAllActive: function (req, callback) {
        var pageNo,param;
        if(req.query.page==undefined){
            pageNo=0;
        }else{
            pageNo=req.query.page-1;
        }
        if(req.param('subject')==undefined){
            param='';
        }else{
            param=req.param('subject');
        }
        activeDao.queryAll(req,param,pageNo, function (result,param) {
                callback(result,param);
        });
    },
    queryAllAdmit: function (req, res, callback) {

        var subject;
        if(req.param('subject')!=undefined){
            subject=req.param('subject');
        }else{
            subject="";
        }
        adminSuperDao.queryAllAdmit(req,[subject], function (result,param) {
            console.log("callback"+param);
            callback(result,param)
        });
    },

    updatePersonInfo: function (req, res, callback) {
        userInfoDao.updatePersonInfo(req, res, function (result) {
            if (result.affectedRows >= 1 && req.session.currentUser.id == req.body.id) {
                req.session.currentUser.name = req.body.name;
                req.session.currentUser.sex = req.body.sex;
                req.session.currentUser.tel = req.body.tel;
                req.session.currentUser.email = req.body.email;
                req.session.currentUser.birthday = req.body.birthday;
                req.session.currentUser.address = req.body.address;
                result = {
                    code: 200,
                    content: result
                };
                callback(result);

            } else {
                result = {
                    code: 0,
                    content: result
                };
                callback(result);
            }
        });
    },


    queryPassword: function (req, res, callback) {
        userDao.queryPassword(req, res, function (result) {
            callback(result)
        });
    },

    updatePassword: function (req, res, callback) {
        userDao.updatePassword(req, res, function (result) {
            callback(result)
        });
    },

    addActivity: function (req, res, callback) {
        activeDetailDao.insert(req, res, function (result) {
            activeDao.insert(req, result.content.insertId, function (result) {
                if (result.affectedRows >= 1) {
                    result = {
                        code: 200,
                        content: result,
                    };
                    callback(result);
                } else {
                    callback("no");
                }
            });
        });
    },

    /**管理员删除活动              有争议*/
    delActivity: function (req, res, callback) {  //����Ա����û�
        activeDetailDao.delActivity(req.query.id, res, function (result) {
            activeDao.delActivity(result.content, res, function (result) {
                // userInfoDao.queryAll(req, res, function (result) {
                callback(result);
                //  })
            });
        })
    },
    //查看邀请人员
    queryInvite: function (req, res, callback) {
        adminSuperDao.queryInvite(req, res, function (result) {
            if (result.length >= 1) {
                result = {
                    code: 200,
                    content: result,
                    activeId: req.query.id,

                };
                callback(result);

            } else {
                callback("no");
            }

        });
    },
    queryStatistic: function (req, callback) {
        activeDetailDao.queryStatistic(req, function (result) {
            if (result.length > 0) {
                callback(result[0]);
            } else {
                result = {
                    code: 0,
                    regisNum: 0,
                    attendNum: 0,
                    inviteNum: 0
                }
                callback(result)
            }
        });

    },
    insertInvite: function (req, res, callback) {
        var temp = req.param('id').split(".")
        for (var i = 0; i < temp.length - 1; i++) {
            admitDao.insertInvite(req, res, temp[i], function (result) {
                activeDao.insertInvite(req, res, function (result) {
                    activeDetailDao.insertInvite(req, res, function (result) {
                    });
                });
            });
        }
        callback(
            code = 200
        )

    },

    updateUser: function (req, res, callback) {
        userInfoDao.updateUser(req, res, function (result) {
            callback(result)
        });
    },


    queryUser: function (req, res, callback) {
        userInfoDao.queryByName(req, res, function (result) {
            callback(result)
        });
    },

    queryActiveByName: function (req, res, callback) {//通过活动主题搜索
        activeDao.queryActiveByName(req, res, function (result) {
            callback(result)
        });
    },
    queryActiveDetail: function (req, callback) {
        activeDao.queryById(req, function (result) {
            activeDetailDao.queryById(result[0], function (detailResult) {
                callback(detailResult)
            });
        })
    },
    changeAdmitStatus: function (req, callback) {
        var status, id;
        var param = req.param('param').split(";");
        status = param[1];
        id = param[3];
        adminSuperDao.changeAdmitStatus(req, status, id, function (result) {
            callback(result)
        });
    },
    cancelInvited: function (req, callback) {
        admitDao.cancelInvited(req, function (result) {
            callback(result)
        });
    },
    updateActiveDetail: function (req, callback) {
        activeDao.updateActive(req, function (result) {
            activeDetailDao.updateDetail(result, function (detailResult) {
                if (detailResult.changedRows > 0) {
                    detailResult = {
                        code: 200,
                        content: detailResult
                    }
                }
                else {
                    detailResult = {
                        code: 0,
                        content: detailResult
                    }
                }
                callback(detailResult)
            });
        })
    },
    queryTel: function (req, res, callback) {
        userInfoDao.queryTel(req, res, function (result) {
            callback(result)
        });
    },

    queryTelExist: function (req, res, callback) { //检查数据库是否存在该电话号码
        userInfoDao.queryTelExist(req, res, function (result) {
            callback(result)
        });
    },
}